home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / PUTIMAGE.C < prev    next >
Encoding:
Text File  |  1991-08-05  |  2.0 KB  |  93 lines

  1.  
  2.                                         /* File       : putimage.c */
  3.                                         /* Entered by : A. Wayner */
  4. # include <graphics.h>
  5. # include <stdlib.h>
  6. # include <alloc.h>
  7.  
  8. main()
  9. {
  10.     int graphdriver = DETECT, graphmode;
  11.     char far *image;
  12.     char      buffer[80];
  13.     short     x, y;
  14.     unsigned numbytes, c= 0;
  15.  
  16.                                             /* Detect adapter type and initialize */
  17.                                             /* graphics system */
  18.     initgraph( &graphdriver, &graphmode, "\\turboc" );
  19.  
  20.                                             /* Draw a stick figure to animate */
  21.     ellipse(5,5,0,360,5,5);
  22.     setcolor(LIGHTCYAN);
  23.     setfillstyle( SOLID_FILL, LIGHTCYAN);
  24.     floodfill(5,5,MAGENTA);
  25.  
  26.     moveto(5,10);
  27.     lineto(5,20);
  28.     lineto(0,30);
  29.  
  30.     moveto(10,30);
  31.     lineto(5,20);
  32.  
  33.     moveto(0,15);
  34.     lineto(0,10);
  35.     lineto(10,15);
  36.  
  37.                                             /* Determine storage needed for image */
  38.     numbytes = (unsigned int ) imagesize( 0,0,10,30 );
  39.  
  40.                                             /* Allocate buffer for image */
  41.     if( (image = (char far *) malloc (numbytes) ) == (char far * ) NULL )
  42.     {
  43.         closegraph();
  44.         printf("Not enough memory for image storage \n");
  45.         exit( 0 );
  46.     }
  47.  
  48.     getimage( 0, 0, 10, 30, image ); /* save the image */
  49.  
  50.                                             /* Now clear screen and draw saved */
  51.                                             /* image at several screen locations */
  52.     cleardevice();
  53.     setcolor( WHITE );
  54.     outtextxy( 10, 10,"Demonstrating animation with putimage");
  55.     x = getmaxx() / 2;
  56.     y = getmaxy() / 2;
  57.  
  58.     putimage(x, y, image, XOR_PUT);
  59.     outtextxy( 10, getmaxy() - 50,
  60.                 "q = exit, h = left, j = down, k = up, l = right");
  61.  
  62.  
  63.     while( c != 'q' )                    /* Perform some animation */
  64.     {
  65.         c = getch();
  66.         putimage( x,y,image,XOR_PUT);    /* First erase at last position */
  67.  
  68.         switch( c )
  69.         {
  70.             case 'h' :                /* 2 pixels left */
  71.                  x -= 2;
  72.                  break;
  73.             case 'l' :                /* 2 pixels right */
  74.                 x += 2;
  75.                 break;
  76.             case 'j' :                 /* 2 pixels down */
  77.                 y += 2;
  78.                 break;
  79.             case 'k' :                /* 2 pixels up */
  80.                 y -= 2;
  81.                 break;
  82.  
  83.         }
  84.  
  85.         putimage( x,y, image, XOR_PUT );    /* Redraw at new position */
  86.  
  87.     }
  88.  
  89.     closegraph();                        /* Close graphics system when done */
  90. }
  91.  
  92.  
  93.